package cn.com.jsepc.rep.demension;

import cn.com.jsepc.rep.comm.Common;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Vector;

import javax.servlet.ServletException;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.jsepc.rep.comm.ConstUtil;
import cn.com.jsepc.rep.vo.DemensionVO;

import com.jarp.db.DAO;

public class demensionGrid {

    //加载数据
    public String getDemeGrid(String start, String limit, String KeyWord) {

        String res = "";
        DAO dao = new DAO();
        try {
            List<DemensionVO> lstResults = new ArrayList<DemensionVO>();
            int iStart = Integer.parseInt(start);
            int iPageSize = Integer.parseInt(limit);
            int iCurPage = iStart / iPageSize + 1;
            StringBuffer sqlN = new StringBuffer();
            sqlN.append("select * from TB_DEMENSION where 1=1");
            if (KeyWord != null && !KeyWord.equals("")) {
                sqlN.append(" and demname like '%" + KeyWord + "%'");
            }
            Vector v = dao.getDataSet(ConstUtil.DBNAME, sqlN.toString(), iCurPage, iPageSize);
            if (v != null) {
                for (int i = 0; i < v.size(); i++) {
                    Hashtable hashRows = (Hashtable) v.get(i);
                    DemensionVO demensionVO = new DemensionVO();
                    demensionVO.setDemid((String) hashRows.get("DEMID"));
                    demensionVO.setDemname((String) hashRows.get("DEMNAME"));
                    demensionVO.setDemcode((String) hashRows.get("DEMCODE"));
                    demensionVO.setHirecdesc((String) hashRows.get("HIRECDESC"));
                    demensionVO.setDimtable((String) hashRows.get("DIMTABLE"));
                    demensionVO.setIdcol((String) hashRows.get("IDCOL"));
                    demensionVO.setNamecol((String) hashRows.get("NAMECOL"));
                    demensionVO.setParentcol((String) hashRows.get("PARENTCOL"));
                    demensionVO.setCodecol((String) hashRows.get("CODECOL"));
                    demensionVO.setDimtype((String) hashRows.get("DIMTYPE"));
                    demensionVO.setIsValid((String) hashRows.get("ISVALID"));
                    demensionVO.setFacdimcol((String) hashRows.get("FACDIMCOL"));
                    demensionVO.setLevelcol((String) hashRows.get("LEVELCOL"));
                    lstResults.add(demensionVO);
                }
            }
            JSONArray json = new JSONArray();
            StringBuffer str1 = new StringBuffer();
            str1.append("select count(1) CNT from TB_DEMENSION");
            if (KeyWord != null && !KeyWord.equals("")) {
                str1.append(" where demname like '%" + KeyWord + "%'");
            }
            String totalCount = dao.getDataValue(ConstUtil.DBNAME, str1.toString());
            json = JSONArray.fromObject(lstResults);
            res = "{totalCount:" + totalCount + ",data:" + json.toString() + "}";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }


    //添加
    public synchronized String DemensionAdd(String itemJsonInfo) throws ServletException {
        String res = "";
        try {
            JSONObject jsonObject = new JSONObject(itemJsonInfo);
            DAO dao = new DAO();
            DemensionVO demensionVO = new DemensionVO();
            String name = jsonObject.getString("demname");
            String code = jsonObject.getString("demcode");
            String facdc = jsonObject.getString("facdimcol").toUpperCase();
            String sql0 = "select * from TB_DEMENSION where demname = '" + name + "' or  demcode= '" + code +"' or facdimcol= '" + facdc + "'";
            Vector v0 = dao.getDataSet(ConstUtil.DBNAME, sql0);
            if (v0 != null) {
                res="false";
            }else{
                    demensionVO.setDemid(jsonObject.getString("demid"));
                    demensionVO.setDemname(jsonObject.getString("demname"));
                    demensionVO.setDemcode(jsonObject.getString("demcode"));
                    demensionVO.setHirecdesc(jsonObject.getString("hirecdesc"));
                    demensionVO.setDimtable(jsonObject.getString("dimtable").toUpperCase());
                    demensionVO.setIdcol(jsonObject.getString("idcol").toUpperCase());
                    demensionVO.setNamecol(jsonObject.getString("namecol").toUpperCase());
                    demensionVO.setParentcol(jsonObject.getString("parentcol").toUpperCase());
                    demensionVO.setCodecol(jsonObject.getString("codecol").toUpperCase());
                    demensionVO.setDimtype(jsonObject.getString("dimtype"));
                    demensionVO.setIsValid(jsonObject.getString("isValid"));
                    demensionVO.setFacdimcol(jsonObject.getString("facdimcol").toUpperCase());
                    demensionVO.setLevelcol(jsonObject.getString("levelcol").toUpperCase());

                    String demid = new Common().getSeqStr("seq_demension");
                    String sql = "insert into  TB_DEMENSION("
                            + "demid ,"
                            + "demname,"
                            + "demcode,"
                            + "hirecdesc,"
                            + "dimtable,"
                            + "idcol,"
                            + "namecol,"
                            + "parentcol,"
                            + "codecol,"
                            + "dimtype,"
                            + "isValid,"
                            + "facdimcol,"
                            + "levelcol) values("
                            + demid + ",'"
                            + demensionVO.getDemname() + "','"
                            + demensionVO.getDemcode() + "','"
                            + demensionVO.getHirecdesc() + "','"
                            + demensionVO.getDimtable() + "','"
                            + demensionVO.getIdcol() + "','"
                            + demensionVO.getNamecol() + "','"
                            + demensionVO.getParentcol() + "','"
                            + demensionVO.getCodecol() + "',"
                            + demensionVO.getDimtype() + ","
                            + demensionVO.getIsValid()+ ",'"
                            + demensionVO.getFacdimcol()+ "','"
                            + demensionVO.getLevelcol() + "')";

                    int bRet = dao.execCommand("Jarp", sql);
                    if (bRet == 1) {
                        res="true";
                    } else {
                        res="error";
                    }
            }

        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return res;
    }

    //修改
    public synchronized String DemensionUpdate(String itemJsonInfo) throws ServletException {
    String res = "";
        try {
            JSONObject jsonObject = new JSONObject(itemJsonInfo);
            DAO dao = new DAO();
            DemensionVO demensionVO = new DemensionVO();
            String id=jsonObject.getString("demid");
            String name = jsonObject.getString("demname");
            String code = jsonObject.getString("demcode");
            String facdc = jsonObject.getString("facdimcol").toUpperCase();
            String sql0 = "select * from TB_DEMENSION  where demid != " + id  + " and (demname= '"+ name + "' or demcode= '" + code + "' or facdimcol= '" + facdc + "')";
            Vector v0 = dao.getDataSet(ConstUtil.DBNAME, sql0);
             if (v0 != null) {
                res="false";
            }else{
                    demensionVO.setDemid(jsonObject.getString("demid"));
                    demensionVO.setDemname(jsonObject.getString("demname"));
                    demensionVO.setDemcode(jsonObject.getString("demcode"));
                    demensionVO.setHirecdesc(jsonObject.getString("hirecdesc"));
                    demensionVO.setDimtable(jsonObject.getString("dimtable").toUpperCase());
                    demensionVO.setIdcol(jsonObject.getString("idcol").toUpperCase());
                    demensionVO.setNamecol(jsonObject.getString("namecol").toUpperCase());
                    demensionVO.setParentcol(jsonObject.getString("parentcol").toUpperCase());
                    demensionVO.setCodecol(jsonObject.getString("codecol").toUpperCase());
                    demensionVO.setDimtype(jsonObject.getString("dimtype"));
                    demensionVO.setIsValid(jsonObject.getString("isValid"));
                    demensionVO.setFacdimcol(jsonObject.getString("facdimcol").toUpperCase());
                    demensionVO.setLevelcol(jsonObject.getString("levelcol").toUpperCase());

                    String demid = demensionVO.getDemid();
                    String demname = demensionVO.getDemname();
                    String demcode = demensionVO.getDemcode();
                    String hirecdesc = demensionVO.getHirecdesc();
                    String dimtable = demensionVO.getDimtable();
                    String idcol = demensionVO.getIdcol();
                    String namecol = demensionVO.getNamecol();
                    String parentcol = demensionVO.getParentcol();
                    String codecol = demensionVO.getCodecol();
                    String dimtype = demensionVO.getDimtype();
                    String isValid = demensionVO.getIsValid();
                    String facdimcol = demensionVO.getFacdimcol();
                    String levelcol = demensionVO.getLevelcol();

                    String sql = "update TB_DEMENSION set demname='" + demname
                            + "',demcode='" + demcode
                            + "',hirecdesc='" + hirecdesc
                            + "',dimtable='" + dimtable
                            + "',idcol='" + idcol
                            + "',namecol='" + namecol
                            + "',parentcol='" + parentcol
                            + "',codecol='" + codecol
                            + "',dimtype=" + dimtype
                            + ",isValid=" + isValid
                            + ",facdimcol='" + facdimcol
                            + "',levelcol='" + levelcol
                            + "' where demid=" + demid;
                    int bRet = dao.execCommand("Jarp", sql);

                    if (bRet == 1) {
                        res="true";
                    } else {
                         res="error";
                    }
             }

        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return  res;
    }

    //删除
    public synchronized String getDemeGrid(String demid) throws ServletException {
        String res = "";
        DAO dao = new DAO();
        try {
        	String sql1 = "select count(1) CC from TB_REP_DEMENSION where dimid ="+demid;
            String cc = dao.getDataValue(ConstUtil.DBNAME, sql1);
            if (cc!=null && Integer.parseInt(cc)>0){
            	res = "haverep";
            }else{
	            String sql = "delete from TB_DEMENSION where demid =" + demid + "";
	            int bRet = dao.execCommand("Jarp", sql);
	            if (bRet == 1) {
	                res = "true";
	            } else {
	                res = "false";
	            }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return res;
    }
    
}
